#!/usr/bin/python


import json
from openpyxl import Workbook


# 打开文件，并确保它最终会被关闭
datas = []
giftIdMap = {}
with open('/Users/lvxin/Downloads/e5ce8384-6f31-415b-8fbc-cb41d5edefc7.json', 'r', encoding='utf-8') as file:
    # 打开文件用于写入，如果文件不存在则创建它
   for line in file:
        # 处理每行
        s = json.loads(line.strip())
        lineArr = s['16_IO'].split("\n")
        for line in lineArr:
            if "/mizos/assets/dao/gain_log.go:170 一个人资产变化日志： gain: " not in line:
                continue
            arr = line.split("/mizos/assets/dao/gain_log.go:170 一个人资产变化日志： gain: ")
            if len(arr) == 2:
                gainData = json.loads(arr[1])
                # print(gainData)
                # print(gainData['player_id'])
                # print(len(gainData['loss_gift']))
                if len(gainData['loss_gift']) > 0:
                    for lossGift in gainData['loss_gift']:
                        lossGift['effective_time'] = '2024-05-27 23:59:59'
                        lossGift['player_id'] = gainData['player_id']
                        del(lossGift['name'])
                        giftIdMap[lossGift['gift_id']] = True
                        datas.append(lossGift)
# with open('/Users/lvxin/Downloads/过期礼物数据.txt', 'w', encoding='utf-8') as file:
#             # print('创建过期礼物数据文件')
#             # 逐行迭代文件对象
#             for data in datas:
#                 file.write(data+"\n")
giftIdStr = ""
for giftId in giftIdMap:
    giftIdStr += ","+str(giftId)
print(giftIdStr.strip(","))



# 创建一个工作簿
wb = Workbook()

# 选择默认的工作表
ws = wb.active

# 给工作表赋予一个标题
ws.title = "2024-05-27"

# 写入一些数据
ws['A1'] = "用户ID"
ws['B1'] = "礼物ID"
ws['C1'] = "礼物名称"
ws['D1'] = "礼物数量"
ws['E1'] = "过期时间"

# 也可以像这样插入行数据

for o in datas:
    # print(data)

    # o = json.loads(data.strip())
    # break
    ws.append([o['player_id'], o['gift_id'], "",o['num'],o['effective_time']])

# 保存工作簿为一个 .xlsx 文件
wb.save("2024-05-27 23:59:59过期礼物数据.xlsx")